package com.winner.data.exception;

import lombok.Data;

import java.io.Serializable;

/**
 * 返回结果
 *
 * @author wangxinle5
 * @since 2022-09-20
 */
@Data
public class Result<T> implements Serializable {

    private int code;

    private String message;

    private T data;

    public Result() {
        this.code = BusinessEnum.SUCCESS.getCode();
        this.message = BusinessEnum.SUCCESS.getMessage();
    }

    private Result(T data) {
        this.code = BusinessEnum.SUCCESS.getCode();
        this.message = BusinessEnum.SUCCESS.getMessage();
        this.data = data;
    }

    private Result(BusinessEnum exceptionEnum) {
        this.code = exceptionEnum.getCode();
        this.message = exceptionEnum.getMessage();
    }

    private Result(BusinessEnum exceptionEnum, T data) {
        this.code = exceptionEnum.getCode();
        this.message = exceptionEnum.getMessage();
        this.data = data;
    }

    public static Result success() {
        return new Result();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> error(T object) {
        Result<T> result = new Result<>();
        result.setCode(BusinessEnum.FAIL.getCode());
        result.setMessage(BusinessEnum.FAIL.getMessage());
        result.setData(object);
        return result;
    }

    public static Result error(BusinessEnum exceptionEnum) {
        return new Result<>(exceptionEnum);
    }

    public static <T> Result<T> error(BusinessEnum exceptionEnum, T t) {
        return new Result<>(exceptionEnum, t);
    }

}
